/**
 * JS to aid in making the admin section usable.
 */

var nav_helper = {
	current_class: "",
	
	mouse_over: function(elem) {
		nav_helper.current_class = $(elem).attr("class");
		$(elem).removeClass(nav_helper.current_class).addClass("ui-state-hover");
	}, // end mouse_over
	
	mouse_out: function(elem) {
		$(elem).removeClass("ui-state-hover").addClass(nav_helper.current_class);
	} // end mouse_out
}; // end nav_helper

var edit_helper = {
	
	cancel_submit: function(formData, jqForm, options) {
		// Takes "beforeSubmit" data from AjaxForm.
		// Check to see if the user cancelled the submission.
		// We have to loop over all the form data to see if cancel is in there.
		var cancel = false;
		for (var i=0; i < formData.length; i++) {
			if (formData[i].name === "cancel") {
				cancel = true;
			}
		}
		
		if(cancel)
		{
			// The user cancelled, so don't submit the form
			$("#ajax_response").text("Submission cancelled");
			$("#ajax_response").addClass("warning");
			return false;
		}
	}, // end cancel_submit
	
	success: function(data) {
		$("#ajax_response").hide();
		$("#ajax_response").addClass(data.message_class);
		$("#ajax_response").html(data.message);
		$("#ajax_response").fadeIn("slow");
		window.setTimeout(function() { $("#ajax_response").fadeOut("slow"); }, 10000)
	} // end success
	
}; // end edit_helper


$(document).ready(function() {
	
	// Set up top navigation
	$("div#site_nav ul li").mouseover(function() {
		nav_helper.mouse_over(this);
	}).mouseout(function() {
		nav_helper.mouse_out(this);
	});
	
	// Set up tabs
	$(".tabs").tabs();
	
	// Help dialogs
	$(".dialog").dialog({
		autoOpen: false,
		bgiframe: true
	});
	$("img[id^='help_icon_']").click(function() {
		var dialog_id = this.id.replace(/help_icon_/, "help_dialog_");
		$("#" + dialog_id).dialog("open");
	});

	
	// Check for HTML editors to Tiny-MCE-ify
	$('textarea.tinymce').tinymce({
		// Location of TinyMCE script
		script_url : '/js/tiny_mce/tiny_mce.js',

		// General options
		theme : "advanced",
		//plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",
		plugins : "safari,style,save,advimage,advlink,iespell,preview,media,searchreplace,contextmenu,paste,directionality,noneditable,xhtmlxtras",

		// Theme options
		theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect,|,cleanup,help",
		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,removeformat,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,code,|,preview,|,forecolor,backcolor,|,iespell",
		theme_advanced_buttons3: "",
		theme_advanced_buttons4: "",
		//theme_advanced_buttons3 : "tablecontrols,|,removeformat,visualaid,|,sub,sup,|,charmap,iespell,media,advhr,|,print,|,fullscreen",
		//theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,pagebreak",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,
		width: 700,

		// Example content CSS (should be your site CSS)
		content_css : "/css/main.css"
		/*
		// Drop lists for link/image/media/template dialogs
		template_external_list_url : "lists/template_list.js",
		external_link_list_url : "lists/link_list.js",
		external_image_list_url : "lists/image_list.js",
		media_external_list_url : "lists/media_list.js",

		// Replace values for the template plugin
		template_replace_values : {
			username : "Some User",
			staffid : "991234"
		}
		*/
	});
	
	// Title/URL helper
	$("#page_title").blur(function() {
		var page_title = $("#page_title").val();
		
		// See if we need to copy to menu title
		var menu_title = $("#menu_title").val();
		if(!menu_title.length)
		{
			$("#menu_title").val( page_title );
		}
		
		// See if we need to copy to meta title
		var meta_title = $("#meta_title").val();
		if(!meta_title.length)
		{
			$("#meta_title").val( page_title );
		}
		
		// See if we need to update the url
		var page_address = $("#page_address").val();
		if(!page_address.length)
		{
			$("#page_address").val( page_title.toLowerCase().replace(/ /, '-') );
		}
	});
	
	// Ajax form submission - is it smart to make this global????????????????????????????
	$("form.ajax").ajaxForm({
		//target: "#ajax_response",
		beforeSubmit: edit_helper.cancel_submit,
		dataType:  'json',
		success: edit_helper.success
	});
	
	// Content Section
	$("div.tree_list div").tree_list();

	
	$("img.edit_submit").click(function() {
		$(this).parent()
		.ajaxForm({
			target: "#main_content"
		})
		.submit();
		
		return(false);
	});
	
});